package se19team1e.dao.impl.property;

import java.util.ArrayList;
import java.util.List;

import se19team1e.dao.abstrakt.jaas.DepartmentDAO;
import se19team1e.dao.abstrakt.property.PropertyDAO;
import se19team1e.entity.DepartmentDTO;
import se19team1e.exception.DataAccessException;

/**
 * Concrete class implementation of the Property DAO, Property DAO is handling
 * department in this implementation,
 * 
 * @author Nasri Bin Najib
 * 
 */
public class DepartmentPropertyDAO extends PropertyDAO<DepartmentDTO> implements
		DepartmentDAO {
	/**
	 * Constructor that calls the super class PropertyDAO to create Department
	 * Property DAO
	 * 
	 * @param jobAdvertProperties
	 */
	public DepartmentPropertyDAO(String jobAdvertProperties) {
		super(jobAdvertProperties);
	}

	/**
	 * Convert DTO into properties given the Department DTO object
	 * 
	 * @return true if create Department DTO is successful)
	 * @throws DataAccessException
	 */
	@Override
	public boolean create(DepartmentDTO dtoType) throws DataAccessException {

		convertDTOIntoProperties(dtoType);
		persist();
		return true;
	}

	/**
	 * 
	 * Deletes an Department object given its ID
	 * 
	 * @param Id
	 *            of the Department object to be deleted
	 * @return true if delete is successful
	 * @throws DataAccessException
	 * 
	 */
	@Override
	public boolean delete(Object id) throws DataAccessException {

		DepartmentDTO departmentDTO = retrieveById(id);
		departmentDTO.setDeleted(true);
		convertDTOIntoProperties(departmentDTO);
		persist();

		return true;
	}

	/**
	 * DAO will retrieve all Department objects in the persistence storage.
	 * 
	 * @return list of Department DataTransfer objects
	 * @throws DataAccessException
	 */
	@Override
	public List<DepartmentDTO> retrieveAll() throws DataAccessException {
		List<DepartmentDTO> departmentDTOList = new ArrayList<DepartmentDTO>();
		int lastSequence = getLastSequence();
		DepartmentDTO departmentDTO = null;
		for (int currentSequence = 1; currentSequence <= lastSequence; currentSequence++) {
			departmentDTO = new DepartmentDTO();
			populateValuesIntoDTO(String.valueOf(currentSequence),
					departmentDTO);
			departmentDTOList.add(departmentDTO);

		}
		return departmentDTOList;
	}

	/**
	 * DAO will retrieve Department objects in the persistence storage based on
	 * the id
	 * 
	 * @param id
	 *            unique id of the object
	 * @return list of Department DataTransfer objects
	 * @throws DataAccessException
	 */
	@Override
	public DepartmentDTO retrieveById(Object id) throws DataAccessException {

		DepartmentDTO departmentDTO = new DepartmentDTO();
		populateValuesIntoDTO((String) id, departmentDTO);
		return departmentDTO;
	}

	/**
	 * DAO will update Department objects in the persistence storage based
	 * 
	 * @param AccountDTO
	 *            Department DataTransfer Object
	 * @return list of Department DataTransfer objects
	 * @throws DataAccessException
	 */
	@Override
	public DepartmentDTO update(DepartmentDTO dto) throws DataAccessException {

		convertDTOIntoProperties(dto);
		persist();
		return retrieveById(dto.getId());
	}

}
